// Copyright (c) 2023 Institute of Software, Chinese Academy of Sciences
// Ripple is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

package ripple.common.tcp.decoder;

import io.netty.buffer.ByteBuf;
import ripple.common.entity.Constants;
import ripple.common.entity.Data;
import ripple.common.helper.TypeHelper;
import ripple.common.tcp.Decoder;
import ripple.common.tcp.Message;
import ripple.common.tcp.MessageType;
import ripple.common.tcp.message.MQSyncRequest;

import java.util.Date;

public class MQSyncRequestDecoder implements Decoder {
    @Override
    public Message decode(ByteBuf byteBuf, MessageType messageType) {
        MQSyncRequest mqSyncRequest = new MQSyncRequest();
        mqSyncRequest.setUuid(TypeHelper.readUuid(byteBuf));
        mqSyncRequest.setMessageUuid(TypeHelper.readUuid(byteBuf));
        mqSyncRequest.setOperationType(TypeHelper.readString(byteBuf));
        mqSyncRequest.setTopic(TypeHelper.readString(byteBuf));
        if (mqSyncRequest.getOperationType().equals(Constants.MQ_MESSAGE)) {
            mqSyncRequest.setData(new Data(TypeHelper.readString(byteBuf)));
            mqSyncRequest.setTowards(TypeHelper.readString(byteBuf));
//        } else if (MQSyncRequest.getOperationType().equals(Constants.MESSAGE_TYPE_INCREMENTAL_UPDATE)) {
//            MQSyncRequest.setBaseMessageUuid(TypeHelper.readUuid(byteBuf));
//            MQSyncRequest.setAtomicOperation(TypeHelper.readString(byteBuf));
//            MQSyncRequest.setValue(TypeHelper.readString(byteBuf));
        }
        mqSyncRequest.setLastUpdate(new Date(byteBuf.readLong()));
        mqSyncRequest.setLastUpdateServerId(byteBuf.readInt());
        return mqSyncRequest;
    }
}